#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <assert.h>

typedef int StackDataType;
class Stack
{
public:
	Stack(int capacity = 4)
	{
		_arr = (StackDataType*)malloc(sizeof(StackDataType) * capacity);
		if (_arr == nullptr)
		{
			perror("malloc fail");
			return;
		}
		_top = 0;
		_capacity = capacity;
	}
	void Push(StackDataType x)
	{
		if (_top == _capacity)
		{
			int newcapccity = _capacity == 0 ? 4 : _capacity * 2;
			StackDataType* newarr = (StackDataType*)realloc(_arr, sizeof(StackDataType) * newcapccity);
			if (newarr == nullptr)
			{
				perror("realloc fail");
				return;
			}
			_arr = newarr;
			_capacity = newcapccity;
		}
		_arr[_top++] = x;
	}
	Stack(const Stack& st)
	{
		_arr = (StackDataType*)malloc(sizeof(StackDataType) * _capacity);
		if (_arr == nullptr)
		{
			perror("malloc fail");
			return;
		}
		memcpy(_arr,st._arr,sizeof(StackDataType) * st._top);
		_top = st._top;
		_capacity = st._capacity;
	}
	~Stack()
	{
		if (_arr)
			free(_arr);
		_arr = nullptr;
		_top = 0;
		_capacity = 0;
	}

	//void Pop()
	//{
	////	assert(_top >= 0 && _top <= capacity);
	//	_top--;
	//}
	//int& Top()
	//{
	//	if (_top >= 1)
	//	{
	//		return _arr[_top - 1];
	//	}
	//}
	//int& Size()
	//{
	//	return _top;
	//}
private:
	StackDataType* _arr;
	int _top;
	int _capacity;
};

using namespace std;

int main()
{
	Stack st1;

	st1.Push(1);
	st1.Push(2);
	st1.Push(3);
	st1.Push(4);
	st1.Push(5);

	//Stack st2 = st1;

	Stack st2(st1);
	return 0;
}